import adModel from "../../../main/view/adView/adModel";
import { soundMgr } from "../../def/sound";
import { UIMgr } from "../../manager/viewMgr";
import { ADMgr } from "../ADManager";
import { WXAPIMgr } from "../concrete/WXAPIManager";

/**
 * 
 * @author: xj.huang
 * @date: 2021/08/03 10:02:44
 */
export default class wxVideo {
    private gameConfig: Game.Config;
    private currVideo: wx.RewardedVideoAd;
    private currVideoType: number;
    private loadMaxNum=10;
    constructor() {

    }
    /**初始化视频 */
    initWxVideo() {
        this.gameConfig = adModel.gameConfig;
        this.onCreateVideo()
    }
    private async onCreateVideo() {
        this.currVideo = await WXAPIMgr.createVideoAd(this.gameConfig.adv_unit_conf.rewarded_video[0], this, this.callBack) as any
        if(!this.currVideo&&this.loadMaxNum>=0){
            this.loadMaxNum--;
            Laya.timer.once(300, this, this.onCreateVideo)
        }
    }
    private async callBack(res) {
        // console.log("callBack")
        if (adModel.gameConfig.ad_move_conf.is_show == 0) return;
        // 没有视频或者视频没看完，并且有广告状态
        let isShowRan = adModel.gameConfig.ad_move_conf.begin_game.treasure_box >= Math.random() * 100
        console.log("callBack1", res,adModel.bannerShowType,isShowRan,this.currVideoType)
        if (!this.currVideo && adModel.bannerShowType || res === 0 && adModel.bannerShowType) {
            if (isShowRan) {
                await this.onOpenBox();
            }
        }
        this.currVideoType = null;
        soundMgr.playBgm()
    }
    private isOpen = false
    private async onOpenBox() {
        if(this.currVideoType===2){
            if(this.isOpen) return;
            this.isOpen = true
            await UIMgr.asyncOpen(ViewRes.VIEW_BOX)
            ADMgr.closeAllBanner();
        }
    }
    /**
     * @param t 
     * 显示视频
     *  - 1显示游戏界面时显示
     *  - 2开始游戏显示
     *  - 3结算游戏显示
     */
    async showVoide(t,callBack?:Function) {
        this.currVideoType = t;
        this.isOpen = false
        let stratP = adModel.gameConfig.ad_move_conf.startView.box_video
        let beginP =adModel.gameConfig.ad_move_conf.begin_game.box_video
        let endP = adModel.gameConfig.ad_move_conf.endView.box_video
        let ran = Math.random() * 100;
        if (this.currVideo && adModel.gameConfig.ad_move_conf.is_show != 0) {
            if (t === 1 && stratP >= ran) {
               return await this.showVideo(callBack)
            }  
            if (t === 2 && beginP >= ran) {
                return await  this.showVideo(callBack)
            } 
             if (t === 3 && endP >= ran) {
                return await  this.showVideo(callBack)
            }else{
                let isShowRan = adModel.gameConfig.ad_move_conf.begin_game.treasure_box >= Math.random() * 100;
                isShowRan&&await this.onOpenBox();
            }
            console.log("有视频", t, stratP, beginP, ran)
        } else if(adModel.gameConfig.ad_move_conf.is_show != 0){ 
            console.log("暂无视频")
            if (!this.currVideo &&adModel.bannerShowType) await this.onOpenBox()
        }

    }
    private async showVideo(callBack){
        soundMgr.stopAllSound()
        return await this.currVideo.show();
    }
}
